package com.epro.dx.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.listenner.OnAddressToChangeListenner;
import com.epro.dx.pullview.PullToRefreshListView;
import com.weipu.common.facade.model.AddressModel;

public class AddressAdapter extends BaseAdapter {

	private List<AddressModel> addressModels;

	private Context mContext;

	private OnAddressToChangeListenner addressListenner;

	private int oldDefaultAddPosition = -1;

	private PullToRefreshListView addressListview;

	private boolean isChooseAddress;

	private Long addressID = -1l;

	public AddressAdapter(Context context, List<AddressModel> models,
			OnAddressToChangeListenner listenner,
			PullToRefreshListView addressListview, boolean isChooseAddress) {
		this.mContext = context;
		this.addressModels = models;
		this.addressListenner = listenner;
		this.addressListview = addressListview;
		this.isChooseAddress = isChooseAddress;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return addressModels.size();
	}

	@Override
	public Object getItem(int index) {
		if (null != addressModels && addressModels.size() > 0) {
			return addressModels.get(index);
		}
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public void setOldPosition(int position){
		this.oldDefaultAddPosition=position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.my_address_book_item, null);
			viewHolder = new ViewHolder();
			viewHolder.name = (TextView) convertView
					.findViewById(R.id.address_username);
			viewHolder.phone = (TextView) convertView
					.findViewById(R.id.address_username_phone);
			viewHolder.mobile = (TextView) convertView
					.findViewById(R.id.address_username_mobile);
			viewHolder.detailAddress = (TextView) convertView
					.findViewById(R.id.address_details);
			if (isChooseAddress) {
				viewHolder.itemLayout = (LinearLayout) convertView
						.findViewById(R.id.item_layout);
				viewHolder.operatLayout = (RelativeLayout) convertView
						.findViewById(R.id.address_operation_layout);
				viewHolder.operatLayout.setVisibility(View.GONE);
				viewHolder.defaultFlag = (TextView) convertView
						.findViewById(R.id.default_addr_flag);
				viewHolder.lineIV = (ImageView) convertView
						.findViewById(R.id.default_addr_flag_up_line);
			} else {

				viewHolder.isDefaultAddress = (RadioButton) convertView
						.findViewById(R.id.address_default);
				viewHolder.addressEdit = (Button) convertView
						.findViewById(R.id.address_edit);
				viewHolder.addressDelete = (Button) convertView
						.findViewById(R.id.address_delete);
			}

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		AddressModel model = null;
		if (null != addressModels) {
			model = addressModels.get(position);
		}
		if (null != model) {
			AddressModel.Address aMAddress = model.getAddress();
			viewHolder.name.setText(aMAddress.getFirstName() + " "
					+ aMAddress.getLastName());
			viewHolder.phone.setText(mContext.getString(
					R.string.address_user_phone, aMAddress.getPhoneNumber()));
			viewHolder.mobile.setText(mContext.getString(
					R.string.address_user_mobile, aMAddress.getMobilePhone()));
			StringBuilder address = new StringBuilder();
			String street2=aMAddress.getStreet2();
			if(null!=street2&&!"".equals(street2)){
				address.append(street2).append(",");
			}
			address.append(aMAddress.getStreet1()).append(",")
			        .append(aMAddress.getCity()).append(",")
					.append(aMAddress.getState()).append(",")
					.append(model.getCountryName()).append("(")
					.append(aMAddress.getPostalCode()).append(")");
			viewHolder.detailAddress.setText(address);
			if (isChooseAddress) {
				if(model.getAddressID() == addressID){
					viewHolder.itemLayout.setBackgroundColor(mContext
							.getResources().getColor(R.color.default_item));
				}else{
					viewHolder.itemLayout.setBackgroundColor(mContext
							.getResources().getColor(R.color.white));
				}
				if (1 == aMAddress.getIsDefault()) {
					viewHolder.defaultFlag.setVisibility(View.VISIBLE);
					viewHolder.lineIV.setVisibility(View.VISIBLE);
				} else {
					viewHolder.lineIV.setVisibility(View.GONE);
					viewHolder.defaultFlag.setVisibility(View.GONE);
				}
				viewHolder.itemLayout.setTag(position);
			} else {
				viewHolder.isDefaultAddress.setTag(position);
				viewHolder.isDefaultAddress
						.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(CompoundButton view,
									boolean isCheck) {
								int position = (Integer) view.getTag();
								if (isCheck) {
									view.setBackgroundResource(R.drawable.address_checked_on);
									if (oldDefaultAddPosition != position) {
										RadioButton radioButtonView = (RadioButton) addressListview
												.findViewWithTag(oldDefaultAddPosition);
										if (null != radioButtonView) {
											// radioButtonView.setChecked(false);
											radioButtonView
													.setBackgroundResource(R.drawable.address_check);
										}
										if (null != addressListenner) {
											addressListenner
													.addressCheckStateChange(
															isCheck, position);
										}
									}
									// oldDefaultAddPosition = position;
								} else {
									view.setBackgroundResource(R.drawable.address_check);
									// if (null != addressListenner) {
									// addressListenner.addressCheckStateChange(
									// isCheck, position);
									// }
								}

							}
						});
				if (1 == aMAddress.getIsDefault()) {
					oldDefaultAddPosition = position;
					if (null != addressListenner) {
						addressListenner.reportDefaultAddress(position);
					}
					viewHolder.isDefaultAddress.setChecked(true);
					viewHolder.isDefaultAddress
							.setBackgroundResource(R.drawable.address_checked_on);
				} else {
					viewHolder.isDefaultAddress.setChecked(false);
					viewHolder.isDefaultAddress
							.setBackgroundResource(R.drawable.address_check);
				}
				viewHolder.addressEdit.setTag(position);
				viewHolder.addressEdit
						.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View view) {
								int position = (Integer) view.getTag();
								if (null != addressListenner
										&& (null != addressModels && addressModels
												.size() > 0)) {
									addressListenner.editAddress(
											addressModels.get(position),
											position);
								}

							}
						});
				viewHolder.addressDelete.setTag(position);
				viewHolder.addressDelete
						.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View view) {
								int position = (Integer) view.getTag();
								if (null != addressListenner
										&& (null != addressModels && addressModels
												.size() > 0)) {
									addressListenner.deleteAddress(
											addressModels.get(position)
													.getAddressID(), position);
								}
							}
						});
			}

		}
		return convertView;
	}

	public void setChoosedAddressID(Long addressID){
		this.addressID = addressID;
	}
	static class ViewHolder {
		TextView name;

		TextView phone;

		TextView mobile;

		TextView detailAddress;

		RadioButton isDefaultAddress;

		Button addressEdit;

		Button addressDelete;

		RelativeLayout operatLayout;

		ImageView lineIV;

		TextView defaultFlag;

		LinearLayout itemLayout;
	}

	public void setAddressToChangeListenner(OnAddressToChangeListenner listenner) {
		this.addressListenner = listenner;
	}

}
